package com.hualan.dao.impl;

import com.hualan.bean.PanghuCommunity;
import com.hualan.bean.PanghuCourierStation;
import com.hualan.dao.CommunityListDao;
import com.hualan.utils.DruidUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CommunityListDaoImpl implements CommunityListDao {



    @Override
    public int addCommunity(PanghuCommunity panghuCommunity) {
        //用来替换 ?
        int row = DruidUtil.update("insert into panghu_community(community_name,community_province,community_city,community_district,community_address,community_latitude,community_longitude,create_time) VALUES(?,?,?,?,?,?,?,NOW())", panghuCommunity.getCommunityName(), panghuCommunity.getCommunityProvince(), panghuCommunity.getCommunityCity(), panghuCommunity.getCommunityDistrict(), panghuCommunity.getCommunityAddress(), panghuCommunity.getCommunityLatitude(), panghuCommunity.getCommunityLongitude());

        return row;
    }

    @Override
    public int updateCommunity(PanghuCommunity panghuCommunity) {
        int row = DruidUtil.update("update panghu_community SET community_name = ?, community_province= ?, community_city = ?, community_district = ?, community_address = ?, community_latitude = ?, community_longitude = ? WHERE community_id = ?", panghuCommunity.getCommunityName(), panghuCommunity.getCommunityProvince(),panghuCommunity.getCommunityCity(),panghuCommunity.getCommunityDistrict(),panghuCommunity.getCommunityAddress(),panghuCommunity.getCommunityLatitude(),panghuCommunity.getCommunityLongitude(),panghuCommunity.getCommunityId());

        return row;
    }

    @Override
    public int deleteCommunityById(int id) {
        int row = DruidUtil.update("delete from panghu_community where community_id = ?", id);

        return row;
    }

    @Override
    public Long selectAllCommunityCount() {
        Map<String, Object> map = DruidUtil.queryOne("select count(*) as count from panghu_community");
        Long count = (Long) map.get("count");

        return count;
    }

    @Override
    public List<PanghuCommunity> communityList(String pageNum, String pageSize, String communityName, String startTime, String endTime) {
        // 判断查询条件
        String condition = " where 1 = 1";
        // 用列表接收有效参数
        List<String> params = new ArrayList<>();
        if (communityName != null && !"".equals(communityName)){
            condition += " and community_name like ?";
            params.add("%" + communityName + "%");
        }
        if (startTime != null && !"".equals(startTime)){
            condition += " and create_time >= ?";
            params.add(startTime);
        }
        if (endTime != null && !"".equals(endTime)){
            condition += " and create_time <= ?";
            params.add(endTime);
        }
        condition += " limit " + pageNum + ", " + pageSize;
        String sql = "SELECT community_id, community_name, community_address, create_time, community_province, community_city, community_district, community_area_code, community_latitude, community_longitude, community_status, community_coordinates FROM panghu_community" + condition;
        System.out.println("完整的查询条件" + sql);

        System.out.println("查询前：" + params);
        // params.toArray(new Object[0])将列表转化成长度为0的object类型数组
        List<PanghuCommunity> communityList = DruidUtil.query(sql, PanghuCommunity.class, params.toArray());

        System.out.println("本次查询到的总条数：" + communityList.size());
        return communityList;

    }
}
